package p400;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * Definition for singly-linked list.
 **/

public class Leetcode_445 {

    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        Stack<Integer> s1 = new Stack<>();
        Stack<Integer> s2 = new Stack<>();
        while (l1 != null) {
            s1.push(l1.val);
            l1 = l1.next;
        }
        while (l2 != null) {
            s1.push(l2.val);
            l2 = l2.next;
        }
        int c = 0;
        ListNode head = null;
        while (!s1.isEmpty() || !s2.isEmpty() || c > 0) {
            int sum = c;
            sum += s1.isEmpty() ? 0 : s1.pop();
            sum += s2.isEmpty() ? 0 : s2.pop();
            ListNode node = new ListNode(sum % 10);
            node.next = head;
            head = node;
            c = sum / 10;
        }
        return head;
    }

    public static void main(String[] args) {
    }
}
